/*
 *  Copyright 2009 Eko Kurniawan Khannedy.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.pos.spatobiz.app.view.karyawan;

import com.pos.spatobiz.app.controller.karyawan.SaringDataKaryawan;
import com.pos.spatobiz.app.model.karyawan.TableModelKaryawan;
import com.pos.spatobiz.app.util.SpringUtilities;
import com.pos.spatobiz.app.view.widget.Table;
import com.pos.spatobiz.app.view.widget.TextBoxTransfer;
import com.pos.spatobiz.app.view.widget.WhiteLabel;
import com.pos.spatobiz.common.dao.KaryawanDao;
import com.pos.spatobiz.common.entity.Karyawan;
import echo.gokil.desktop.swing.Panel;
import echo.gokil.desktop.worker.DesktopWorker;
import java.util.List;
import java.util.Locale;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *
 * @author echo
 */
public class DataKaryawan extends Panel implements ApplicationContextAware, InitializingBean {

    private static final long serialVersionUID = 1L;

    private ApplicationContext applicationContext;

    private TableModelKaryawan tableModelKaryawan;

    private SaringDataKaryawan saringDataKaryawan;

    /** Creates new form DataKaryawan */
    public DataKaryawan() {
        tableModelKaryawan = new TableModelKaryawan();
        initComponents();

        table.getTable().setModel(tableModelKaryawan);
    }

    public void setSaringDataKaryawan(SaringDataKaryawan saringDataKaryawan) {
        this.saringDataKaryawan = saringDataKaryawan;
        textSaring.getDocument().addDocumentListener(saringDataKaryawan);
    }

    public TableModelKaryawan getTableModelKaryawan() {
        return tableModelKaryawan;
    }

    public Table getTable() {
        return table;
    }

    @Override
    public void onOpen() throws Exception {
        super.onOpen();
        new LoadKaryawan(true, this).execute();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        whiteLabel1 = new WhiteLabel();
        textSaring = new TextBoxTransfer();
        table = new Table();

        whiteLabel1.setText("Saring :");
        whiteLabel1.setName("whiteLabel1"); // NOI18N

        textSaring.setName("textSaring"); // NOI18N

        table.setName("table"); // NOI18N

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                    .addComponent(table, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 664, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(whiteLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(textSaring, GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(whiteLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(textSaring, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addComponent(table, GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected Table table;
    protected TextBoxTransfer textSaring;
    protected WhiteLabel whiteLabel1;
    // End of variables declaration//GEN-END:variables

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void afterPropertiesSet() throws Exception {
        setTitle(applicationContext.getMessage("datakaryawan.title", null, Locale.getDefault()));
    }

    public class LoadKaryawan extends DesktopWorker<List<Karyawan>, Void> {

        private DataKaryawan datakaryawan;

        public LoadKaryawan(boolean blockInput, DataKaryawan dataKaryawan) {
            super(blockInput);
            this.datakaryawan = dataKaryawan;
        }

        @Override
        public boolean beforeDone() {
            try {
                List<Karyawan> list = get();
                datakaryawan.getTableModelKaryawan().removeAll();
                datakaryawan.getTableModelKaryawan().addKaryawan(list);
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        @Override
        public void afterDone() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        protected List<Karyawan> doInBackground() throws Exception {
            KaryawanDao karyawanDao = SpringUtilities.getKaryawanDao();
            List<Karyawan> list = karyawanDao.selectKaryawan();
            return list;
        }
    }
}
